load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
load("//bazel:defs.bzl", "rust_test_suite_with_extra_srcs")

package(default_visibility = ["//visibility:public"])

# See rs/nervous_system/feature_test.md
BASE_DEPENDENCIES = [
    # Keep sorted.
    "//rs/interfaces/registry",
    "//rs/ledger_suite/common/ledger_core",
    "//rs/ledger_suite/icp:icp_ledger",
    "//rs/ledger_suite/icrc1",
    "//rs/ledger_suite/icrc1/index-ng",
    "//rs/ledger_suite/icrc1/ledger",
    "//rs/ledger_suite/icrc1/tokens_u64",
    "//rs/nervous_system/agent",
    "//rs/nervous_system/clients",
    "//rs/nervous_system/common",
    "//rs/nervous_system/proto",
    "//rs/nervous_system/root",
    "//rs/nns/cmc",
    "//rs/nns/common",
    "//rs/nns/governance/api",
    "//rs/nns/sns-wasm",
    "//rs/registry/proto_data_provider",
    "//rs/sns/cli",
    "//rs/sns/governance",
    "//rs/sns/governance/api",
    "//rs/sns/init",
    "//rs/sns/root",
    "//rs/sns/swap",
    "//rs/test_utilities/load_wasm",
    "//rs/types/base_types",
    "//rs/types/management_canister_types",
    "@crate_index//:assert_matches",
    "@crate_index//:candid",
    "@crate_index//:futures",
    "@crate_index//:hex",
    "@crate_index//:ic-management-canister-types",
    "@crate_index//:itertools",
    "@crate_index//:lazy_static",
    "@crate_index//:pretty_assertions",
    "@crate_index//:prost",
    "@crate_index//:rust_decimal",
    "@crate_index//:tempfile",
    "@crate_index//:tokio",
    "@crate_index//:url",
] + select({
    "@rules_rust//rust/platform:wasm32-unknown-unknown": [],
    "//conditions:default": [
        "//packages/icrc-ledger-types:icrc_ledger_types",
        "//packages/pocket-ic",
        "//rs/crypto/sha2",
        "//rs/nervous_system/common/test_keys",
        "//rs/nervous_system/common/test_utils",
        "//rs/nns/constants",
        "//rs/protobuf",
        "//rs/registry/canister",
        "//rs/registry/keys",
        "//rs/registry/routing_table",
        "//rs/registry/subnet_type",
        "//rs/registry/transport",
        "//rs/rust_canisters/canister_test",
        "//rs/test_utilities",
        "@crate_index//:maplit",
        "@crate_index//:num-traits",
        "@crate_index//:rustc-hash",
        "@crate_index//:serde",
    ],
})

DEPENDENCIES_WITH_TEST_FEATURES = BASE_DEPENDENCIES + [
    "//rs/nns/governance:governance--test_feature",
    "//rs/nns/handlers/root/impl:root--test_feature",
] + select({
    "@rules_rust//rust/platform:wasm32-unknown-unknown": [],
    "//conditions:default": [
        "//rs/nns/test_utils:test_utils--test_feature",
        "//rs/nns/gtc:gtc--test_feature",
        "//rs/sns/test_utils:test_utils--test_feature",
    ],
})

MACRO_DEPENDENCIES = [
    # Keep sorted.
    "@crate_index//:rust_decimal_macros",
]

DEV_DEPENDENCIES = []

ALIASES = {}

DEV_DATA = [
    # Keep sorted
    "//rs/ledger_suite/icp/archive:ledger-archive-node-canister-wasm",
    "//rs/ledger_suite/icp/ledger:ledger-canister-wasm",
    "//rs/ledger_suite/icrc1/archive:archive_canister",
    "//rs/ledger_suite/icrc1/index-ng:index_ng_canister",
    "//rs/ledger_suite/icrc1/ledger:ledger_canister",
    "//rs/nns/cmc:cycles-minting-canister",
    "//rs/nns/governance:governance-canister",
    "//rs/nns/governance:governance-canister-test",
    "//rs/nns/gtc:genesis-token-canister",
    "//rs/nns/handlers/root/impl:root-canister",
    "//rs/nns/sns-wasm:sns-wasm-canister",
    "//rs/node_rewards/canister:node-rewards-canister",
    "//rs/pocket_ic_server:pocket-ic-server",
    "//rs/registry/canister:registry-canister",
    "//rs/registry/canister:registry-canister-test",
    "//rs/rust_canisters/xrc_mock:xrc_mock_canister",
    "//rs/sns/governance:sns-governance-canister",
    "//rs/sns/governance:sns-governance-canister-test",
    "//rs/sns/root:sns-root-canister",
    "//rs/sns/swap:sns-swap-canister",
    "//rs/tests/test_canisters/image_classification:image-classification",
    "//rs/universal_canister/impl:universal_canister.wasm.gz",
    "@cycles-ledger.wasm.gz//file",
    "@mainnet_canisters//:governance.wasm.gz",
    "@mainnet_canisters//:ledger.wasm.gz",
    "@mainnet_canisters//:lifeline.wasm.gz",
    "@mainnet_canisters//:node-rewards.wasm.gz",
    "@mainnet_canisters//:registry.wasm.gz",
    "@mainnet_canisters//:root.wasm.gz",
    "@mainnet_canisters//:sns-wasm.wasm.gz",
    "@mainnet_canisters//:sns_archive.wasm.gz",
    "@mainnet_canisters//:sns_governance.wasm.gz",
    "@mainnet_canisters//:sns_index.wasm.gz",
    "@mainnet_canisters//:sns_ledger.wasm.gz",
    "@mainnet_canisters//:sns_root.wasm.gz",
    "@mainnet_canisters//:swap.wasm.gz",
]

DEV_ENV = {
    "CARGO_MANIFEST_DIR": "rs/nns/integration_tests",
    "CYCLES_LEDGER_WASM_PATH": "$(rootpath @cycles-ledger.wasm.gz//file)",
    "CYCLES_MINTING_CANISTER_WASM_PATH": "$(rootpath //rs/nns/cmc:cycles-minting-canister)",
    "GOVERNANCE_CANISTER_WASM_PATH": "$(rootpath //rs/nns/governance:governance-canister)",
    "GOVERNANCE_CANISTER_TEST_WASM_PATH": "$(rootpath //rs/nns/governance:governance-canister-test)",
    "REGISTRY_CANISTER_WASM_PATH": "$(rootpath //rs/registry/canister:registry-canister)",
    "REGISTRY_CANISTER_TEST_WASM_PATH": "$(rootpath //rs/registry/canister:registry-canister-test)",
    "IC_ICRC1_ARCHIVE_WASM_PATH": "$(rootpath //rs/ledger_suite/icrc1/archive:archive_canister)",
    "IC_ICRC1_INDEX_NG_WASM_PATH": "$(rootpath //rs/ledger_suite/icrc1/index-ng:index_ng_canister)",
    "IC_ICRC1_LEDGER_WASM_PATH": "$(rootpath //rs/ledger_suite/icrc1/ledger:ledger_canister)",
    "IMAGE_CLASSIFICATION_CANISTER_WASM_PATH": "$(rootpath //rs/tests/test_canisters/image_classification:image-classification)",
    "LEDGER_CANISTER_WASM_PATH": "$(rootpath //rs/ledger_suite/icp/ledger:ledger-canister-wasm)",
    "LEDGER_ARCHIVE_NODE_CANISTER_WASM_PATH": "$(rootpath //rs/ledger_suite/icp/archive:ledger-archive-node-canister-wasm)",
    "MAINNET_IC_ICRC1_LEDGER_WASM_PATH": "$(rootpath @mainnet_canisters//:sns_ledger.wasm.gz)",
    "MAINNET_IC_ICRC1_ARCHIVE_WASM_PATH": "$(rootpath @mainnet_canisters//:sns_archive.wasm.gz)",
    "MAINNET_IC_ICRC1_INDEX_NG_WASM_PATH": "$(rootpath @mainnet_canisters//:sns_index.wasm.gz)",
    "MAINNET_SNS_WASM_CANISTER_WASM_PATH": "$(rootpath @mainnet_canisters//:sns-wasm.wasm.gz)",
    "MAINNET_NODE_REWARDS_CANISTER_WASM_PATH": "$(rootpath @mainnet_canisters//:node-rewards.wasm.gz)",
    "MAINNET_ICP_LEDGER_CANISTER_WASM_PATH": "$(rootpath @mainnet_canisters//:ledger.wasm.gz)",
    "MAINNET_GOVERNANCE_CANISTER_WASM_PATH": "$(rootpath @mainnet_canisters//:governance.wasm.gz)",
    "MAINNET_LIFELINE_CANISTER_WASM_PATH": "$(rootpath @mainnet_canisters//:lifeline.wasm.gz)",
    "MAINNET_ROOT_CANISTER_WASM_PATH": "$(rootpath @mainnet_canisters//:root.wasm.gz)",
    "MAINNET_REGISTRY_CANISTER_WASM_PATH": "$(rootpath @mainnet_canisters//:registry.wasm.gz)",
    "NEURON_CSV_PATH": "rs/nns/integration_tests/src/neurons.csv",
    "GENESIS_TOKEN_CANISTER_WASM_PATH": "$(rootpath //rs/nns/gtc:genesis-token-canister)",
    "SNS_WASM_CANISTER_WASM_PATH": "$(rootpath //rs/nns/sns-wasm:sns-wasm-canister)",
    "NODE_REWARDS_CANISTER_WASM_PATH": "$(rootpath //rs/node_rewards/canister:node-rewards-canister)",
    "SNS_GOVERNANCE_CANISTER_WASM_PATH": "$(rootpath //rs/sns/governance:sns-governance-canister)",
    "SNS_GOVERNANCE_CANISTER_TEST_WASM_PATH": "$(rootpath //rs/sns/governance:sns-governance-canister-test)",
    "MAINNET_SNS_GOVERNANCE_CANISTER_WASM_PATH": "$(rootpath @mainnet_canisters//:sns_governance.wasm.gz)",
    "SNS_ROOT_CANISTER_WASM_PATH": "$(rootpath //rs/sns/root:sns-root-canister)",
    "MAINNET_SNS_ROOT_CANISTER_WASM_PATH": "$(rootpath @mainnet_canisters//:sns_root.wasm.gz)",
    "SNS_SWAP_CANISTER_WASM_PATH": "$(rootpath //rs/sns/swap:sns-swap-canister)",
    "MAINNET_SNS_SWAP_CANISTER_WASM_PATH": "$(rootpath @mainnet_canisters//:swap.wasm.gz)",
    "ROOT_CANISTER_WASM_PATH": "$(rootpath //rs/nns/handlers/root/impl:root-canister)",
    "XRC_MOCK_WASM_PATH": "$(rootpath //rs/rust_canisters/xrc_mock:xrc_mock_canister)",
    "POCKET_IC_BIN": "$(rootpath //rs/pocket_ic_server:pocket-ic-server)",
    "UNIVERSAL_CANISTER_WASM_PATH": "$(rootpath //rs/universal_canister/impl:universal_canister.wasm.gz)",
}

rust_library(
    name = "nervous_system_integration_tests",
    testonly = True,
    srcs = glob(["src/**/*.rs"]),
    aliases = ALIASES,
    crate_name = "ic_nervous_system_integration_tests",
    proc_macro_deps = MACRO_DEPENDENCIES,
    version = "0.9.0",
    deps = DEPENDENCIES_WITH_TEST_FEATURES,
)

rust_test_suite_with_extra_srcs(
    name = "integration_tests_test",
    timeout = "long",
    srcs = glob(
        ["tests/**/*.rs"],
        exclude = [
            # The following tests are flaky when run as part of this test suite.
            # So they have been moved to dedicated bazel targets below
            # such that we can have different CPU reservations and better logging.
            "tests/sns_upgrade_test_utils.rs",
            "tests/upgrade_everything_test.rs",
            "tests/advance_target_version_upgrades_all_canisters_test.rs",
            "tests/upgrade_existing_sns_test.rs",
            "tests/deploy_fresh_sns_test.rs",
            "tests/sns_release_qualification_legacy.rs",
            "tests/sns_upgrade_test_utils_legacy.rs",
            "tests/upgrade_sns_controlled_canister_with_large_wasm.rs",
            "tests/custom_upgrade_path.rs",
            "tests/sns_topics.rs",
            "tests/sns_extension_test.rs",
        ],
    ),
    aliases = ALIASES,
    data = DEV_DATA,
    env = DEV_ENV,
    extra_srcs = ["tests/sns_upgrade_test_utils.rs"],
    proc_macro_deps = MACRO_DEPENDENCIES,
    tags = [
        "cpu:6",
    ],
    deps = [":nervous_system_integration_tests"] + DEPENDENCIES_WITH_TEST_FEATURES + DEV_DEPENDENCIES,
)

rust_test(
    name = "upgrade_everything_test",
    timeout = "long",
    srcs = [
        "tests/sns_upgrade_test_utils.rs",
        "tests/upgrade_everything_test.rs",
    ],
    aliases = ALIASES,
    crate_root = "tests/upgrade_everything_test.rs",
    data = DEV_DATA,
    env = DEV_ENV | {"RUST_TEST_NOCAPTURE": "1"},
    proc_macro_deps = MACRO_DEPENDENCIES,
    tags = [
        "cpu:6",
    ],
    deps = [":nervous_system_integration_tests"] + DEPENDENCIES_WITH_TEST_FEATURES + DEV_DEPENDENCIES,
)

rust_test(
    name = "advance_target_version_upgrades_all_canisters_test",
    timeout = "long",
    srcs = [
        "tests/advance_target_version_upgrades_all_canisters_test.rs",
    ],
    aliases = ALIASES,
    data = DEV_DATA,
    env = DEV_ENV | {"RUST_TEST_NOCAPTURE": "1"},
    proc_macro_deps = MACRO_DEPENDENCIES,
    tags = [
        "cpu:4",
    ],
    deps = [":nervous_system_integration_tests"] + DEPENDENCIES_WITH_TEST_FEATURES + DEV_DEPENDENCIES,
)

rust_test(
    name = "upgrade_existing_sns_test",
    timeout = "long",
    srcs = [
        "tests/upgrade_existing_sns_test.rs",
    ],
    aliases = ALIASES,
    data = DEV_DATA,
    env = DEV_ENV | {"RUST_TEST_NOCAPTURE": "1"},
    proc_macro_deps = MACRO_DEPENDENCIES,
    tags = [
        "cpu:4",
    ],
    deps = [":nervous_system_integration_tests"] + DEPENDENCIES_WITH_TEST_FEATURES + DEV_DEPENDENCIES,
)

rust_test(
    name = "deploy_fresh_sns_test",
    timeout = "long",
    srcs = [
        "tests/deploy_fresh_sns_test.rs",
    ],
    aliases = ALIASES,
    data = DEV_DATA,
    env = DEV_ENV | {"RUST_TEST_NOCAPTURE": "1"},
    proc_macro_deps = MACRO_DEPENDENCIES,
    tags = [
        "cpu:4",
    ],
    deps = [":nervous_system_integration_tests"] + DEPENDENCIES_WITH_TEST_FEATURES + DEV_DEPENDENCIES,
)

rust_test(
    name = "sns_release_qualification_legacy",
    timeout = "long",
    srcs = [
        "tests/sns_release_qualification_legacy.rs",
        "tests/sns_upgrade_test_utils_legacy.rs",
    ],
    aliases = ALIASES,
    data = DEV_DATA,
    env = DEV_ENV | {"RUST_TEST_NOCAPTURE": "1"},
    proc_macro_deps = MACRO_DEPENDENCIES,
    tags = [
        "cpu:4",
    ],
    deps = [":nervous_system_integration_tests"] + DEPENDENCIES_WITH_TEST_FEATURES + DEV_DEPENDENCIES,
)

rust_test(
    name = "upgrade_sns_controlled_canister_with_large_wasm",
    timeout = "long",
    srcs = [
        "tests/upgrade_sns_controlled_canister_with_large_wasm.rs",
    ],
    aliases = ALIASES,
    data = DEV_DATA,
    env = DEV_ENV | {"RUST_TEST_NOCAPTURE": "1"},
    proc_macro_deps = MACRO_DEPENDENCIES,
    tags = [
        "cpu:4",
    ],
    deps = [":nervous_system_integration_tests"] + DEPENDENCIES_WITH_TEST_FEATURES + DEV_DEPENDENCIES,
)

rust_test(
    name = "custom_upgrade_path",
    timeout = "long",
    srcs = [
        "tests/custom_upgrade_path.rs",
    ],
    aliases = ALIASES,
    data = DEV_DATA,
    env = DEV_ENV | {"RUST_TEST_NOCAPTURE": "1"},
    proc_macro_deps = MACRO_DEPENDENCIES,
    tags = [
        "cpu:4",
    ],
    deps = [":nervous_system_integration_tests"] + DEPENDENCIES_WITH_TEST_FEATURES + DEV_DEPENDENCIES,
)

rust_test(
    name = "sns_topics",
    timeout = "long",
    srcs = [
        "tests/sns_topics.rs",
    ],
    aliases = ALIASES,
    data = DEV_DATA,
    env = DEV_ENV | {"RUST_TEST_NOCAPTURE": "1"},
    proc_macro_deps = MACRO_DEPENDENCIES,
    tags = [
        "cpu:4",
    ],
    deps = [":nervous_system_integration_tests"] + DEPENDENCIES_WITH_TEST_FEATURES + DEV_DEPENDENCIES,
)

rust_test(
    name = "sns_extension_test",
    timeout = "long",
    srcs = [
        "tests/sns_extension_test.rs",
    ],
    aliases = ALIASES,
    data = DEV_DATA + [
        "@kong_backend_canister//file",
        "@kongswap-adaptor-canister//file",
    ],
    env = DEV_ENV | {
        "KONG_BACKEND_CANISTER_WASM_PATH": "$(rootpath @kong_backend_canister//file)",
        "KONGSWAP_ADAPTOR_CANISTER_WASM_PATH": "$(rootpath @kongswap-adaptor-canister//file)",
        "RUST_TEST_NOCAPTURE": "1",
    },
    proc_macro_deps = MACRO_DEPENDENCIES,
    tags = [
        "cpu:4",
    ],
    deps = [":nervous_system_integration_tests"] + DEPENDENCIES_WITH_TEST_FEATURES + DEV_DEPENDENCIES + [
        "//rs/sns/treasury_manager",
    ],
)
